/**
 * 
 */
$(function(){
	loadOrderByType();
	
});

/**
 * 根据状态加载订单
 */
function loadOrderByType(obj){
	var orderList;
	var url;
	var params = {};
	if(obj != null || obj != ""){
		params.condition = {};
		params.condition.orderState= $(obj).attr("data-orderState");
		params.size = 4;
				
		console.log(params);
	}
	
	$.ajax({
		type:"get",
		url:basePath + "/order/list",
		data:{"page":JSON.stringify(params)},
		async:true,
		success:function(data){
			if(data){
				orderList = data.obj.records;
				console.log(orderList);
				if(obj){
					if($(obj).attr("data-orderState") == "未提交"){
						var $allOrder = $("#waitPay");
					}else if($(obj).attr("data-orderState") == "已提交"){
						var $allOrder = $("#waitSend");
					}else if($(obj).attr("data-orderState") == "未完成"){
						var $allOrder = $("#noComplete");
					}else if($(obj).attr("data-orderState") == "已完成"){
						var $allOrder = $("#complete");
					}else{
						var $allOrder = $("#allOrder");
					}
				}else{
					var $allOrder = $("#allOrder");
				}
				
			    $allOrder.html("");
				for(var i in orderList){
					var $allOrderList = $("<div class='allOrderList'></div>");
					var $titleInfo = $("<div class='titleInfo'><ul class='clearfix'><li>下单时间：<span class='orderTime'>"+(new Date(orderList[i].createTime)).Format("yyyy-MM-dd")+"</span> </li>" +
							"<li>订单编号：<span class='orderCode'>"+ orderList[i].orderCode + "</span></li>" +
									"<li class='fl-r' style='text-align:right;color:#B4A078;cursor:pointer;'><span class='orderCode' onclick='removeOrder(this)' data-orderid='"+orderList[i].id+"'><i class='fa fa-trash'></i>&nbsp;删除</span></li></ul></div>");
					var $commodityMessge = $("<div class='commodityMessge'></div>");
					
					$.ajax({
						type:"get",
						url:basePath + "/order/orderDetail/" + orderList[i].orderCode,
						async:false,
						success:function(data){
							if(data){
								for(var j in data.obj){
									var $ul = $("<ul><li class='clearfix'><div class='goodImg fl-l'>" +
											"<img src='"+data.obj[j].images.split(",")[0]+"' alt='' /></div><span class='goodDetail'>"+data.obj[j].goodsName+"</span>" +
											"<span class='num'>包裹×<i class='goodsNum'>"+data.obj[j].quantity+"</i></span></li>" +
											"<li>￥<span class='goodsPrice'>"+data.obj[j].total+"</span></li><li><span class='goodsStat'>"+orderList[i].orderState+"</span></li>" +
											"</ul>");
									$commodityMessge.append($ul);
								}
								
							}
						}
					});
					
					$allOrderList.append($titleInfo);
					$allOrderList.append($commodityMessge);
					$allOrder.append($allOrderList);
				}
				
			}
		}
	});
	return orderList;
}

/**
 * 删除订单
 */
function removeOrder(obj){
	var orderId = $(obj).attr("data-orderid");
	$("#removeModal").modal("show");
	
	$("#sureRemove").on("click",function(){
		$.ajax({
			type:"post",
			url:basePath + "/order/del/" + orderId,
			async:false,
			success:function(data){
				if(data.status == "200"){
					$("#removeModal").modal("hide");
//					loadOrderByType();
					location.reload();
					
				}
			}
		});
	});
	
}



/**
 * 格式化时间
 */
Date.prototype.Format = function (fmt) { //author: tony  
    var o = {  
        "M+": this.getMonth() + 1, //月份  
        "d+": this.getDate(), //日  
        "h+": this.getHours(), //小时  
        "m+": this.getMinutes(), //分  
        "s+": this.getSeconds(), //秒  
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度  
        "S": this.getMilliseconds() //毫秒  
    };  
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));  
    for (var k in o)  
        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));  
    return fmt;  
};























